const mysql = require('../db/mysql57');

const repairDao = {
    addRepair(reqId, uid, deviceId, time, description, processMethod, changeAccessory, repairman) {
        let sql = `insert into DeviceRepair(deviceId, time, description, processMethod, changeAccessory, repairman) values(?, ?, ?, ?, ?, ?)`;
        let data = [ deviceId, time, description, processMethod, changeAccessory, repairman ];
        return mysql.sqlExec(sql, data);
    },
    getAllRepair(reqId, uid) {
        let sql = `select
                dr.id as id,
                d.devID as deviceId,
                dt.name as deviceName,
                dp.id as departmentId,
                dp.name as department,
                DATE_FORMAT(d.warrantyEndDate, '%Y-%m-%d') as warrantyExpiry,
                DATE_FORMAT(dr.time, '%Y-%m-%d %H:%i:%s') as time,
                dr.description as description,
                dr.processMethod as processMethod,
                dr.changeAccessory as changeAccessory,
                dr.repairman as repairman
            from DeviceRepair as dr
            left join Device as d on dr.deviceId = d.id
            left join DeviceType as dt on d.deviceTypeId = dt.id
            left join Department as dp on d.departmentId = dp.id
            where dr.is_del = ?
            order by dr.id desc`;
        let data = [ 'false' ];
        return mysql.sqlExec(sql, data);
    },
    updateRepair(reqId, uid, id, time, description, processMethod, changeAccessory, repairman) {
        let sql = `update DeviceRepair
            set time = ?,
                description = ?,
                processMethod = ?,
                changeAccessory = ?,
                repairman = ?
            where id = ? and is_del = ?`;
        let data = [ time, description, processMethod, changeAccessory, repairman, id, 'false' ];
        return mysql.sqlExec(sql, data);
    },
    delRepair(reqId, uid, id) {
        let sql = `update DeviceRepair
            set is_del = ?
            where id = ?`;
        let data = [ 'true', id ];
        return mysql.sqlExec(sql, data);
    }
};

module.exports = repairDao;